package at.catalysts.imgClassifier.transformer;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import at.catalysts.imgClassifier.dataFinders.ImgReader;
import at.jaki.ubb.crawling.api.DataTransformer;
import at.jaki.ubb.crawling.model.CEEntity;
import at.jaki.ubb.crawling.model.DataTransformerResult;
import at.jaki.ubb.crawling.model.RawData;
import at.jaki.ubb.crawling.model.Target;
import at.jaki.ubb.crawling.stlib.dataFinder.SimpleHttpClient;

public class HtmlImgExtractor implements DataTransformer<String, String, CEEntity, String> {

	private static final Logger LOG = Logger.getLogger(HtmlImgExtractor.class);

	public static final String TYPE_TR = "typeTr";

	public DataTransformerResult<String, String, CEEntity> transformData(
			RawData<String> rawData) {
		LOG.info("Getting images from " + rawData.getId());

		Collection<Target<String>> targetCollection = new LinkedList<Target<String>>();

		Pattern pt = Pattern.compile("[<]img.+?src[=][\"'](.+?)[\"']");
		Matcher m = pt.matcher(rawData.getData());
		while (m.find()) {
			String s = m.group(1);
			String domain = "";
			if (!s.startsWith("http")){
				//Pattern.compile("(http[:][/][/][^/]+)[/]").matcher(rawData.getId()).find()
				Pattern pt2 = Pattern.compile("(http[:][/][/][^/]+[/])");
				Matcher m2 = pt2.matcher(rawData.getId());
				if (m2.find()){
					domain = m2.group(1);
				}
			}
			String ss = domain + s;
			targetCollection.add(new Target<String>(0.95D, ss, ImgReader.TYPE_IMG_URL, ss));
		}

		DataTransformerResult<String, String, CEEntity> dtr = new DataTransformerResult<String, String, CEEntity>(null, null, targetCollection);
		return dtr;
	}

	public void init() {

	}

	public List<String> getTypes() {
		return Collections.singletonList(SimpleHttpClient.RESULT_TYPE);
	}

}
